package kr.saeildamil.androidstudy_second.actionbar;

import kr.saeildamil.andrordstudy.second.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class ActionTab extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch21_actiontab);
		
		ActionBar ab = getActionBar();
		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		for(int i=0; i<4;i++){
			ActionBar.Tab tab =  ab.newTab();
			String capStr = "Tab" + (i+1);
			tab.setText(capStr);
			TabFragment frag = TabFragment.newInstance(capStr);
			tab.setTabListener(new TabListener(frag));
			ab.addTab(tab);
		}
		
		if (savedInstanceState != null){
			int selTab = savedInstanceState.getInt("selTab");
			ab.setSelectedNavigationItem(selTab);
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("selTab", getActionBar().getSelectedNavigationIndex());
	}
	
	private class TabListener implements ActionBar.TabListener{
		private Fragment mFragment;
		
		public TabListener(Fragment mFragment) {
			super();
			this.mFragment = mFragment;
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.replace(R.id.ch21_actiontabfragment_tabparent, mFragment, "tag");
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(mFragment);
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			
		}
		
	}
}
